require "sinatra"
require 'nori'

#Sinatra config
set :static, true

get '/' do
 
 get_drinks
 erb :template

end

get '/drink/:drinkkey' do |drinkkey|

 @drinks = get_drinks
 @drink = @drinks.find{|d| d['key'] == drinkkey }
 if @drink then erb :drink 
 else erb "Drink not found"
 end

end

get '/make/:drinkkey' do  |key|
  
  if `pidof santabarbot`!="" then #Make the drink
    stuff = `santabarbot #{key}`
    redirect "/" 
  else
    erb :busy  
  end
  
end

helpers do
  def get_drinks
    #Loadup the XML
    xmlfile = File.read("RecipeList.xml")
    @xml = Nori.parse(xmlfile)
    @drinks = @xml["Recipes"]["Drink"]
  end
end

